package guitforl.parsers.xml

import _root_.guitforl.invocation.UserProcess
import scala.xml.{NodeSeq}
import scala.collection.mutable.{HashMap}
import guitforl.wrappers.{ButtonWrapper, TableWrapper}
import guitforl.data.readers.FileReader
import scala.io.Source
import java.io.File
import guitforl.wrappers._
import guitforl.parsers.xml.ImplementationXMLKeywords._
import guitforl.parsers.xml.XMLWidgetNodeParser._
import javax.swing.JPanel

/**
 * Created by IntelliJ IDEA.
 * User: artem
 * Date: Jun 25, 2010
 * Time: 9:46:27 PM
 * To change this template use File | Settings | File Templates.
 */
object ImplementationReader {
  def read(tabXMLBlock: NodeSeq, tab: TabWrapper) = {
    val pathToExe = (tabXMLBlock \ "pathToExe").text
    val prefix = (tabXMLBlock \ "runPrefix").text
    getComposites(tabXMLBlock).foreach {
      compositeNode => (compositeNode \ "_").foreach { node =>
        val areaComposite = tab.getComposite(getWidgetName(compositeNode));
        node match
        {
          case <edit>{_*}</edit> => {
            val edit = WrappersFactory.createEditFromXML(node)
            areaComposite.add(edit)
            tab.addEdit(edit)
            }
          case <label>{_*}</label> => {
	          val label = WrappersFactory.createLabelFromXML(node)
		        areaComposite.add(label)
	          tab.addUpdatebleWidget(label)

	        }
          case <chart>{_*}</chart> => {
	          val newChart = WrappersFactory.createChartFromXML(node)
            areaComposite.add(newChart)
	          tab.addUpdatebleWidget(newChart)
          }
          case <table>{_*}</table> => {
	          val table = WrappersFactory.createTableFromXML(node)
	          areaComposite.add(table)
	          tab.addUpdatebleWidget(table)
          }
          case <button>{updates @ _*}</button> => {
            areaComposite.add(new ButtonWrapper(getWidgetName(node), prefix + " " + pathToExe, tab.getEdits, (node \ BUTTON_REQUIRE_NODE).map(_.text).toList, (node \ BUTTON_UPDATE_NODE).map(_.text).toList, tab.update))
          }
        }
      }
    }
  }

	private def getComposites(XMLBlock: NodeSeq) = {
		(XMLBlock \ COMPOSITE_NODE)
	}
	
}